package com.viego.financialrecommendsystem.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.viego.financialrecommendsystem.entity.*;
import com.viego.financialrecommendsystem.mapper.*;
import com.viego.financialrecommendsystem.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    //这是用户的 mapper
    @Resource
    UserMapper userMapper;

    //这是访问记录对象的 mapper
    @Resource
    AccessRecordMapper accessRecordMapper;

    @Resource
    ArticleMapper articleMapper;

    @Resource
    DiscussMapper discussMapper;

    @Resource
    CommentMapper commentMapper;

    @Override
    public User login(User user) {
        return userMapper.login(user);
    }


    @Override
    public Integer getassets(Long userId) {
        return userMapper.getassets(userId);
    }

    @Override
    public boolean addRecord(List<AccessRecord> list) {
        return accessRecordMapper.insertList(list) == list.size();

    }

    @Override
    public boolean save(User user) {
        return userMapper.updateById(user) == 1;
    }

    @Override
    public User info(Long userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public List<Article> getArticle(String userId) {
        return userMapper.getArticle(userId);
    }

    @Override
    public List<Discuss> getDiscuss(String userId) {
        return userMapper.getDiscuss(userId);
    }


    @Override
    public boolean deleteArticle(long articleId) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getArticleId,articleId);
        return articleMapper.delete(queryWrapper) == 1;
    }

    @Override
    public boolean deleteDiscuss(long discussId) {
        LambdaQueryWrapper<Discuss> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Discuss::getDiscussId,discussId);
        return discussMapper.delete(queryWrapper) == 1;
    }

    @Override
    public boolean isUsed(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);
        return userMapper.selectList(queryWrapper).size() > 0;
    }

    @Override
    public boolean active(String code) {
        return userMapper.active(code) == 1;
    }

    @Override
    public boolean register(User user) {
        return userMapper.insert(user) == 1;
    }

    //TODO 写完下面这个函数之后，感觉自己好蠢啊，我最开始设计数据库的时候，就没有设计好，一遍写一遍设计的，一遍改，
    //TODO 但是现在整个项目都要结束了，发现了这样的一个 BUG 真的是没心情去动数据库了，只能写这样的蠢代码了
    //TODO 其实就应该在 comment 表中加上 user_id 字段的
    //TODO 而且我在 UserService 中注入了好多其它的 Mapper 虽然这些被注入的 Mapper 在项目启动的时候，就已经
    //TODO 在 Spring IOC 容器中被创建了，但是还是决定自己写的代码好乱呀，跟 * 山一样。
    @Override
    public void updateCommentImg(Long currentId, String string_avatar_path) {
        User user = userMapper.selectById(currentId);
        commentMapper.updateCommentImg(user.getUsername(),string_avatar_path);
    }

    @Override
    public List<String> getAllAccessRecord(Long currentId) {
        return accessRecordMapper.getAllAccessRecordType(currentId);
    }

    @Override
    public boolean charge(String userId, String charge) {
        return userMapper.charge(userId,charge) == 1;
    }
}
